<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="pickTemp" v-if="showPick">
        <el-select v-model="queryParams.pickTemp" @change="pickChangeHandler">
          <el-option
            v-for="(item,index) in pickResult"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="deptName" v-if="showDept">
        <el-input
          v-model="deptName"
          placeholder="请选择部门"
          @focus="openPickDept"
        />
      </el-form-item>
      <el-form-item prop="examineUserName">
        <el-input
          v-model="queryParams.examineUserName"
          placeholder="请输入考核人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="examineCycle">
        <el-select v-model="queryParams.examineCycle" placeholder="请选择考核周期" clearable @change="getCycleValue">
          <el-option
            v-for="dict in dict.type.examine_cycle"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="cycleVal==='month'" prop="pickCycle">
        <el-date-picker
          v-model="queryParams.pickCycleText"
          type="month"
          placeholder="选择周期"
          style="width: 100%"
          format="yyyy 年 MM 月"
          value-format="yyyy年 M月"
          :picker-options="examineYearOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="cycleVal==='quarter'" prop="pickCycle">
        <quarter  @change="getQuarter"></quarter>
      </el-form-item>
      <el-form-item v-if="cycleVal==='half_year'" prop="pickCycle">
        <halfyear  @change="getHalfyear"></halfyear>
      </el-form-item>
      <el-form-item v-if="cycleVal==='year'" prop="pickCycle">
        <el-date-picker
          v-model="queryParams.pickCycleText"
          type="year"
          value-format="yyyy年"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['examine:performance:add']"
        >提交考核
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commonList">
      <el-table-column label="考核部门" align="center" prop="ancestorNames" width="210px">
        <template slot-scope="scope">
          <span>{{ scope.row.ancestorNames ? scope.row.ancestorNames : '中层管理考核' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考核周期" align="center" prop="pickCycleText" width="130px"/>
      <el-table-column label="优秀人数" align="center" prop="excellentCount"/>
      <el-table-column label="良好人数" align="center" prop="goodCount"/>
      <el-table-column label="称职人数" align="center" prop="qualifiedCount"/>
      <el-table-column label="基本称职人数" align="center" prop="almostCount"/>
      <el-table-column label="不称职人数" align="center" prop="unqualifiedCount"/>
      <el-table-column label="考核人姓名" align="center" prop="examineUserName"/>
      <el-table-column label="考核时间" align="center" prop="examineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examineTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['examine:performance:edit']"
            v-if="currentUserId === scope.row.examineUserId"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['examine:performance:query']"
            v-if="currentUserId !== scope.row.examineUserId"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <dept-dialog :orgType="orgType" :orgData="orgData" v-if="showOrgTree" :show="showOrgTree" @close="showOrgTree=false"
                 @save="saveOrgValue" :orgIds="orgIds"></dept-dialog>
  </div>
</template>

<script>
import {listPerformance, delPerformance} from "@/api/examine/performance/api";
import quarter from "@/views/tool/quarter";
import halfyear from "@/views/tool/halfyear";
import deptDialog from "@/components/DeptPicker";
import {treeselect} from "@/api/system/dept";

export default {
  name: "Index",
  dicts: ['examine_cycle'],
  components: {
    quarter,
    halfyear,
    deptDialog
  },
  data() {
    return {
      currentUserId: Number,
      showPick: false,
      showDept: false,
      pickResult: [{id: 0, name: '全部'}, {id: 1, name: '中层管理考核'}, {id: 2, name: '组织机构'},],
      deptName: '',
      showOrgTree: false,
      orgData: [],
      orgIds: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      cycleVal: '',
      // 通用考核表格数据
      commonList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examineUserName: undefined,
        examineCycle: undefined,
        pickCycle: undefined,
        deptSearchStr: undefined,
        pickTemp: 0,
        examineDeptId:'',
        pickCycleText:''
      },
      orgType: '',
      examineYearOptions: {
        disabledDate: (time) => {
          const date = new Date()
          const year = date.getFullYear()
          const timeYear = time.getFullYear()
          return year > timeYear + 3
        }
      }
    };
  },
  created() {
    this.currentUserId = this.$store.state.user.id
    this.checkUserCategory()
    this.getTreeselect();
    this.getList();
  },
  methods: {
    checkUserCategory() {
      this.showDept = false
      this.showPick = false
      if (this.$store.state.user.category.indexOf('1') !==-1) {
        // 高层类型
        this.showPick = true
      } else {
        this.showDept = true
      }
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.orgData = response.data;
      });
    },
    /** 查询通用考核列表 */
    getList() {
      this.loading = true;
      listPerformance(this.queryParams).then(response => {
        this.commonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    pickChangeHandler(data) {
      if (data == 2) {
        this.openPickDept()
        this.showDept = true
        this.queryParams.examineDeptId=''
      } else if (data == 1) {
        this.showDept = false
        this.deptName = ''
        this.orgIds = []
        this.queryParams.deptSearchStr = ''
        this.queryParams.examineDeptId=-999
      } else {
        this.cycleVal = '';
        this.orgIds = []
        this.queryParams.deptSearchStr = ''
        this.queryParams.examineDeptId=''
        this.deptName = ''
      }
    },
    getCycleValue(data) {
      this.cycleVal = data;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.cycleVal = '';
      this.orgIds = []
      this.queryParams.deptSearchStr = ''
      this.queryParams.examineDeptId=''
      this.queryParams.pickCycleText=''
      this.deptName = ''
      this.checkUserCategory()
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: "/performance/add"});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path: "/performance/add", query: {id: row.id}});
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.$router.push({path: "/performance/view", query: {id: row.id}});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除通用考核周期为"' + row.pickCycleText + '"的数据？删除后无法恢复').then(function () {
        return delPerformance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/common/export', {
        ...this.queryParams
      }, `common_${new Date().getTime()}.xlsx`)
    },
    // 季度选择回调
    getQuarter(val,text) {
      // this.queryParams.pickCycle = val
      this.queryParams.pickCycleText = text
    },
    // 半年度选择回调
    getHalfyear(val,text) {
      // this.queryParams.pickCycle = val
      this.queryParams.pickCycleText = text
    },
    openPickDept() {
      if (this.queryParams.deptSearchStr) {
        this.orgIds = this.queryParams.deptSearchStr.split(",");
      }
      this.showOrgTree = true
    },
    saveOrgValue(values) {
      this.showOrgTree = false
      if (values.length > 0) {
        this.queryParams.deptSearchStr = values.map(m => m.id).join(",")
        this.deptName = values.map(m => m.deptName).join(",")
      } else {
        this.orgIds = []
        this.queryParams.deptSearchStr = ''
        this.deptName = ''
      }
    }
  }
}
;
</script>
<style>
.el-input--small .el-input__inner {
  height: 32px;
  line-height: 32px;
  cursor: pointer !important;
}
</style>
